﻿using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Profiling;
using XLua;
using Yoozoo.Core.Extensions.Engine;
using Yoozoo.Gameplay.RTS.Proto;
using Yoozoo.UI.YGUI;

namespace Yoozoo.Gameplay.RTS
{
    public class WonderNpcHud : MonsterHud
    {
        private WonderNpcHudLod wonderNpcHudLod0;
        private GameObject wonderNpcHudLod0Obj;

        public override void Initialize(int team)
        {
            BaseInitialize(team);

            var hudManager = ArmyManager.Instance.HudManager;
            if (Lod0 == null)
            {
                Lod0 = hudManager.GetHud(HudManager.MarchHudLod0);
            }

            _lod0Visible = Lod0.activeInHierarchy;
            MarchHudLod0 = Lod0?.GetComponent<MarchHudLod0>();
            MarchHudLod0.hudRef = this;
            if (wonderNpcHudLod0Obj == null)
            {
                wonderNpcHudLod0Obj = hudManager.GetHud(HudManager.WonderNpc);
            }


            wonderNpcHudLod0 = wonderNpcHudLod0Obj?.GetComponent<WonderNpcHudLod>();
            InitAlpha();
        }

        public override void Dispose()
        {
            // 释放
            var hudManager = ArmyManager.Instance.HudManager;
            MarchHudLod0.PlayerNameBg.gameObject.transform.localPosition = Vector3.zero;
            hudManager.RecoverHud(HudManager.MarchHudLod0, Lod0);
            Lod0 = null;
            MarchHudLod0.Dispose();
            RemoveCommanderHead(true);
            MarchHudLod0 = null;
            if (wonderNpcHudLod0Obj)
            {
                hudManager.RecoverHud(HudManager.WonderNpc, wonderNpcHudLod0Obj);
                wonderNpcHudLod0Obj = null;
            }

            Lod1 = null;

            MarchHudLod0 = null;
            
            MarchEntity = null;
            hasSetParent = false;

            BaseDispose();
        }

        protected override void SelfInit()
        {
            
        }
        
        public override void Hide()
        {
            if (wonderNpcHudLod0Obj)
            {
                wonderNpcHudLod0Obj.transform.position = invisiblePosition;
            }
            base.Hide();
        }
        protected override void InitParentScaleAndPosition()
        {

            hasSetParent = true;
        }
        //TODO:这些偏移最好能做到prefab里面，不要每次都加
        private Vector3 wonderNpcHudLod0ObjOffset = new Vector3(-0.23f, 0.057f, -0.22f);
        public override void UpdateFollowPos()
        {
            
            if (hasSetParent)
            {
                var parentPos = MarchEntity.Transform.position;
                if (Lod0 != null)
                {
                    Lod0.transform.localPosition = parentPos;
                }

                if (wonderNpcHudLod0Obj != null)
                {
                    wonderNpcHudLod0Obj.transform.localPosition = wonderNpcHudLod0ObjOffset + parentPos;
                }
                
            }
        }
        protected override void OnPlayerNameVisibleChanged(bool value)
        {
            if (MarchHudLod0 != null)
            {
                MarchHudLod0.PlayerNameBg.gameObject.SetActive(value);
                if (value)
                {
                    MarchHudLod0.PlayerNameText.color = MarchHelper.TeamTxtColors[_team];
                    MarchHudLod0.SetNickNameColor(MarchHelper.TeamTxtColors[_team]);
                }
                MarchHudLod0.SetNickNameVisiable(value);
            }

            if (wonderNpcHudLod0 != null)
            {
                wonderNpcHudLod0.SetActive(value);
            }
        }
        
        public override void UpdatePlayerName()
        {
            // 初始化，设置不变的部分，玩家名、阵营以及英雄头像
            // 设置玩家名和颜色，仅lod0
            //MarchHudLod0.PlayerNameText.text = MarchEntity.RtsServerInfo.name;
            SetNickName(MarchEntity.RtsServerInfo.name);
            MarchHudLod0.PlayerNameText.color = MarchHelper.TeamTxtColors[_team];
            MarchHudLod0.SetNickNameColor(MarchHelper.TeamTxtColors[_team]);
            MarchHudLod0.PlayerNameBg.gameObject.transform.localPosition = new Vector3(0,85,0);
            MarchHudLod0.SetNickNamePosition(true);
            //MarchHudLod0.PlayerNameBg.gameObject.SetActive(false);
            //MarchHudLod0.OtherBattleInfo.SetBasicInfo(_troopMarch.lod1.monsterName);

            
            
            
            MarchHudLod0.StateIcon.enabled = false;
            MarchHudLod0.StateIconBg.enabled = false;
            string name;
            if (MarchEntity.RtsServerInfo.npcType == RtsNpcType.NPC_MIRACLE_GUARD)
            {
                name = "icon_map_WonderTroops_1";
            }
            else if (MarchEntity.RtsServerInfo.npcType == RtsNpcType.NPC_MIRACLE_REINFORCE)
            {
                name = "icon_map_WonderTroops_2";
            }
            else
            {
                name = "icon_map_WonderTroops_3";
            }
            
            wonderNpcHudLod0.SetLevelBg(name);
        }

        private string bgIconName;

        public override void SetTeam(int team)
        {
            _team = team;
            /*if (wonderNpcHudLod0)
            {
                wonderNpcHudLod0.SetTeam(team);
            }*/
            if (baseArmyInfo)
            {
                var teamColor = MarchHelper.TeamColors[team];
                baseArmyInfo.SetTextColor(teamColor);
                baseArmyInfo.ChangeSoldierCountVisiableByTeam(team);
                baseArmyInfo.SetCount(MarchEntity.MarchInfo.SoldierCount);
            }
            MarchHudLod0.PlayerNameText.color = MarchHelper.TeamTxtColors[team];
            MarchHudLod0.SetNickNameColor(MarchHelper.TeamTxtColors[team]);
        }

        protected override void SetLod0Visible(MarchContext marchContext,bool force = false)
        {
            var isShowLod0 = marchContext.LodLevel < WorldEntry.MaxShowMarchLodLevel;
            bool lod0Visible = isShowLod0 || GetHeadShowValue(EHeadShowChange.IsBattleState);
            if (_lod0Visible != lod0Visible || force)
            {
                if (!lod0Visible)
                {
                    Lod0.transform.localScale = Vector3.zero;
                }
                else
                {
                    base.SetHudLodScale(LodHudType.LodLowArmyInfo, Lod0, marchContext);
                }
                _lod0Visible = lod0Visible;
                MarchHudLod0.PlayerNameBg.gameObject.SetActive(lod0Visible);
                MarchHudLod0.SetNickNameVisiable(_lod0Visible);
                UpdatePlayerName();
            }
        }

    }
}
